package com.andnnl.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 做接口访问限制，访问一次获取一批数据，
 * 如果每个用户访问有数据，每秒最多访问10次，
 * 如果没数据，直接返回空，1秒最多访问1次，
 * 接口支持多用户同时访问的，每个用户需要自己的RateLimiter
 * Created by chenss on 2019/12/30.
 */
public class ThreadUtil {
    public static void sleep(long ms) {
        try {
//            System.out.println("sleep start....");
            Thread.sleep(ms);
//            System.out.println("sleep end....");
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        int x = 012;//8进制
        System.out.println(x);
        System.out.println("start....");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            ThreadUtil.sleep(2000);
            return 10 / 2;
        });
//       System.out.println(future.join());
       System.out.println(future.get());
//        System.out.println(future.getNow(10));
        System.out.println("sleep 5000");
        Thread.sleep(5000);
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println("runAsync"));
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "supplyAsync");

        System.out.println(future1.get());//null
        System.out.println(future2.get());//supplyAsync

    }
}
